package com.heima.behavior.service.impl;
import java.util.Date;

import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadUtil.AppThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 存储点赞文章数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.非空判断
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取登录用户
        ApUser user = AppThreadLocalUtil.getUser();
        //3.判断用户是否存在，是否游客
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
        //4.游客无权操作
        if (user.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //5.redis查询是否有用户点赞文章
        String like_key = BehaviorConstants.USER_LIKES_ + dto.getArticleId() + "";
        Boolean likeKeyFlag = stringRedisTemplate.opsForHash().hasKey(like_key, user.getId() + "");
        //5.1是，并且正在执行的是点赞操作，直接返回
        if (likeKeyFlag && dto.getOperation() == 0) {
            return ResponseResult.okResult("点赞文章成功");
        }
        //5.1.1是，并且执行的是取消点赞，删除redis，mongodb
        if (likeKeyFlag && dto.getOperation() == 1) {
            //删除redis
            stringRedisTemplate.opsForHash().delete(like_key, user.getId() + "");
            //删除mongodb
            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("userId").is(user.getId()));
            mongoTemplate.remove(query, ApLikesBehavior.class);

            return ResponseResult.okResult("已取消点赞文章");
        }
        //5.2否，正在执行点赞操作 新增redis，mongodb
        if (!likeKeyFlag && dto.getOperation() == 0) {
            //新增redis
            stringRedisTemplate.opsForHash().put(like_key, user.getId() + "", "1");
            //新增mongodb
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setId(ObjectId.get().toHexString());
            apLikesBehavior.setUserId(Long.valueOf(user.getId()));
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType((short) 0);
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior);
        }
        return ResponseResult.okResult("点赞文章成功");

    }
}